//
//  VEHadJionListViewController.m
//  VEMoneyPorject
//
//  Created by BlueWind on 2018/9/7.
//  Copyright © 2018年 陈建. All rights reserved.
//

#import "VEHadJionListViewController.h"
#import "VETAssociationTableViewCell.h"
#import "VECreatAssociationTableViewCell.h"
#import "VEAssociationDetailViewController.h"



@interface VEHadJionListViewController ()

@end

@implementation VEHadJionListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    // 可以刷新可以加载更多
    self.refreshType = CJBaseTableVcRefreshTypeRefreshAndLoadMore;
    [self cj_refresh];
}


- (void)cj_refresh{
    [super cj_refresh];
    [self loadData];
}

- (void)loadData{
    [super loadData];
    [self requestDataWithType:1];
    
}

- (void)cj_loadMore{
    [super cj_loadMore];
    NSLog(@"%ld",self.page);
    [self requestDataWithType:2];
    
}

- (void)requestDataWithType:(NSInteger)type{
    
    NSString *userId = [[CJUserInfoManager sharedManager] currentUserInfo].USERID;
    
    if ([NSObject isNilOrNull:userId]) {
        userId = @"";
    }
    
    NSString *url = [NSString stringWithFormat:@"%@/VEService/GetJoincommunity.do",kPublicInterfaceAPI];
    CJBaseRequest *request = [CJBaseRequest cj_requestWithUrl:url isPost:YES withParameters:@{@"Userid":userId,@"Page":@(self.page)}];
    [request cj_sendRequestWithCompletion:^(id response, BOOL success, NSString *message) {
        self.isSuccessNet = YES;
 
        [self cj_endRefresh];
        [self cj_endLoadMore];
        self.isSuccessNet = YES;
        if (![NSObject isNilOrNull:response]) {
            NSArray *arr = response;
            if (arr.count == 0 && type != 1) {
                self.page -=1;
                return;
            }
            if (type == 1) {
                [self.dataArray removeAllObjects];
                self.dataArray = [VEAssionModel mj_objectArrayWithKeyValuesArray:arr];
            } else {
                NSArray *arr1 = [VEAssionModel mj_objectArrayWithKeyValuesArray:arr];
                [self.dataArray addObjectsFromArray:arr1];
            }
        }
        
        [self.tableView reloadData];
        
    }];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger)cj_numberOfSections {
    return 1;
}

/**
 某个cell的数量
 */
- (NSInteger)cj_numberOfRowsInsection:(NSInteger)section {
    return self.dataArray.count + 1;
}

- (CGFloat)cj_cellHeightAtIndexPath:(NSIndexPath *)indexPath {
    return 85;
}

/**
 某行的cell
 */
- (CJBaseTableViewCell *)cj_cellAtIndexPath:(NSIndexPath *)indexPath {
    

    if (indexPath.row == 0) {
        VECreatAssociationTableViewCell *cell = [VECreatAssociationTableViewCell cellWithTableView:self.tableView];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        return cell;
    } else {
        WEAKSELF
        VETAssociationTableViewCell *cell =[VETAssociationTableViewCell cellWithTableView:self.tableView];
        cell.isHadJoin = YES;
        VEAssionModel *model = self.dataArray[indexPath.row -1];
        cell.model = model;
        cell.isCrearDT = self.isCrearDT;
        if (self.isCrearDT == YES) {
            cell.jionNumCount.hidden = YES;
            cell.joinNum.hidden = YES;
            cell.joinButton.hidden = NO;
            cell.jionAssionAction = ^{
                if (self->_seletedAssionGetAssionId) {
                    self->_seletedAssionGetAssionId(model);
                }
                [weakSelf.navigationController popViewControllerAnimated:YES];
            };
        } else {
            cell.jionNumCount.hidden = NO;
            cell.joinNum.hidden = YES;
            cell.joinButton.hidden = YES;
        }
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        return cell;
    }
    
    
}

- (void)cj_didSelectCellAtIndexPath:(NSIndexPath *)indexPath cell:(CJBaseTableViewCell *)cell{
    if (indexPath.row == 0) {
        [CJUIKit showWithString:@"请联系客服人员"];
    }
    if (indexPath.row != 0) {
        VEAssociationDetailViewController *vc = [VEAssociationDetailViewController new];
        vc.title = @"介绍";
        VEAssionModel *model = self.dataArray[indexPath.row-1];
        vc.assionId = model.COMMUNITYID;
        vc.isJoin = @"已加入";
        [self.navigationController pushViewController:vc animated:YES];
    }

}
@end
